﻿#include "CarAndQRCodeVerify.h"
#include "ui_carandQRcodeverify.h"

CarAndQRCodeVerify::CarAndQRCodeVerify(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CarAndQRCodeVerify)
{
    ui->setupUi(this);
    time = new QLabel(ui->frameBottom);
    time->setGeometry(10, 10, 200, 40);
    time->setStyleSheet("QLabel{font: 20px; color: rgb(25, 25, 112);}");
    signal = new QLabel(ui->frameBottom);
    signal->setGeometry(700, 18, 41, 22);
    signal->setStyleSheet("QLabel{background-color:transparent;}");
    qCode = new WidgetQrCode(ui->frame);
    qCode->move(0, 0);
    qCode->resize(146, 146);
    ui->VINcharge->hide();

#if (GUN_AMOUNT == 1)
    ui->pushButton->hide();
#endif

    QTimer *UnlockEnable = new QTimer(this);
    connect(UnlockEnable,SIGNAL(timeout()), this, SLOT(UnlockEnableSlot()));
//    UnlockEnable->start(500);
#if (PRODUCER != 3)
    ui->buttonUnlock->hide();
#endif

    // temp not use: || PRODUCER == 6 2021-07-04
#if (PRODUCER == 5 || PRODUCER == 3)
    ui->VINcharge->show();
#endif

    QDir dir(IMAGE_PATH);
    if(dir.exists())
    {
        ui->frameTop->setStyleSheet(LOGO_STY);
    }
    cardrrorinfo = new err_record;
    frist_error = 0;

    CheckAppOrSwipCard_ChargingType = new QTimer(this);
    connect(CheckAppOrSwipCard_ChargingType, SIGNAL(timeout()), this, SLOT(AppChargingSignalOrSwopCard_Received()));

    connect(stackLayout, SIGNAL(currentChanged(int)), this, SLOT(drawQRCode(int)));
    connect(stackLayout, SIGNAL(currentChanged(int)), this, SLOT(UserChooseChargingType(int)));
    connect(&updateqr, SIGNAL(timeout()), this, SLOT(drawQR_emit()));
    connect(this, SIGNAL(onsendQRCodeValue(QString)), qCode, SLOT(onHandleQrCodeValue(QString)));
    connect(&mt, SIGNAL(sendData(QString,int,uint)), this, SLOT(receiveCardInfo_25Page(QString,int,uint)));//接收刷卡线程发出的信号
    SwipCardMachine_Error = new QTimer(this);
    connect(SwipCardMachine_Error, SIGNAL(timeout()), this, SLOT(ChangeSwipPicture()));
    SwipCardMachine_Error->start(1000);
}

CarAndQRCodeVerify::~CarAndQRCodeVerify()
{
    delete ui;
    free(cardrrorinfo);
}

void CarAndQRCodeVerify::ChangeSwipPicture()
{
    if(card->errcode != 0)//读卡器通讯不正常
    {
        emit SwipCardMachine_Error_Signal();
        protocol_exit_seek_card_status();
        ui->labelCard->setStyleSheet(QString("QLabel{border-image: url(:/Res/image/cardstop1.png);}"));
        if(frist_error == 0)
        {
            cardrrorinfo->guntab = focus_gun_-1;

            cardrrorinfo->time[0] = QDateTime::currentDateTime().toString("yyyyMMddhhmmss").mid(0,2).toInt();
            cardrrorinfo->time[0] = cardrrorinfo->time[0]/10*6+cardrrorinfo->time[0];
            cardrrorinfo->time[1] = QDateTime::currentDateTime().toString("yyyyMMddhhmmss").mid(2,2).toInt();
            cardrrorinfo->time[1] = cardrrorinfo->time[1]/10*6+cardrrorinfo->time[1];
            cardrrorinfo->time[2] = QDateTime::currentDateTime().toString("yyyyMMddhhmmss").mid(4,2).toInt();
            cardrrorinfo->time[2] = cardrrorinfo->time[2]/10*6+cardrrorinfo->time[2];
            cardrrorinfo->time[3] = QDateTime::currentDateTime().toString("yyyyMMddhhmmss").mid(6,2).toInt();
            cardrrorinfo->time[3] = cardrrorinfo->time[3]/10*6+cardrrorinfo->time[3];
            cardrrorinfo->time[4] = QDateTime::currentDateTime().toString("yyyyMMddhhmmss").mid(8,2).toInt();
            cardrrorinfo->time[4] = cardrrorinfo->time[4]/10*6+cardrrorinfo->time[4];
            cardrrorinfo->time[5] = QDateTime::currentDateTime().toString("yyyyMMddhhmmss").mid(10,2).toInt();
            cardrrorinfo->time[5] = cardrrorinfo->time[5]/10*6+cardrrorinfo->time[5];
            cardrrorinfo->time[6] = QDateTime::currentDateTime().toString("yyyyMMddhhmmss").mid(12,2).toInt();
            cardrrorinfo->time[6] = cardrrorinfo->time[6]/10*6+cardrrorinfo->time[6];

            cardrrorinfo->type=102;

            protocol_updateERR(cardrrorinfo);

            frist_error = 1;
        }
    }
    else
    {
        ui->labelCard->setStyleSheet(QString("QLabel{border-image: url(:/Res/image/way1.png);}"));
        frist_error = 0;
    }
}

void CarAndQRCodeVerify::drawQRCode(int current)
{
    if(current == cardAndQRCodeVerifyPage)
    {
#if (PRODUCER == 3 || PRODUCER == 6)
        /*动态更新二维码*/
        if (focus_gun_ != 0)
        {
            drawQR_emit();
            updateqr.start(500);
        }
    }
    else
    {
        updateqr.stop();
#else
        if (focus_gun_ != 0)
        {
            emit onsendQRCodeValue(GUN_STATE[focus_gun_-1].m_QrCodeNum);
        }
#endif
    }
}

void CarAndQRCodeVerify::UserChooseChargingType(int current)
{
    if(current == cardAndQRCodeVerifyPage)
    {
        if(focus_gun_ != 0)
        {
            ui->_focus_gun_->setText(QString::number(focus_gun_));
            CheckAppOrSwipCard_ChargingType->start(100);//定时检测App充电信号送达
            if(!mt.isRunning() && card->errcode == 0)
            {
                emit get_Manage_CardNum(Management_CardNum);
//                mt.start();
            }
        }
    }
    else
    {
        if(current != chargeWatchingDCPage && current != WaitSwipCardPage)//这两个界面都需要刷卡的功能,避免混乱排除掉这两个界面
        {
            protocol_exit_seek_card_status();
            if(mt.isRunning())
                mt.wait();
        }
        if(CheckAppOrSwipCard_ChargingType->isActive())
            CheckAppOrSwipCard_ChargingType->stop();
    }
}

void CarAndQRCodeVerify::receiveCardInfo_25Page(QString CardNum, int value, uint availabelMoney)
{
    qDebug("读卡标志: value = %d\n", value);
    qDebug()<<"接收到的卡号是:"<<CardNum << ";  "<<tr("卡内余额是: ")<<availabelMoney;
    //int YCTcardflag = 0;
    if(stackLayout->currentIndex() == cardAndQRCodeVerifyPage && focus_gun_ != 0 && value == 0)
    {
        for(int i=0; i<5; i++)
        {
            if(CardNum == Management_CardNum[i] && !CardNum.trimmed().isEmpty() && !Management_CardNum[i].isEmpty())
            {
                if(i>=0 && i<=1)
                    qDebug()<<"A类充电管理卡:"<<i;
                else if(i>=2 && i<=4)
                    qDebug()<<"B类充电管理卡:"<<i;

                emit _25Page_Swip(CardNum, focus_gun_);
                //YCTcardflag = 1;
                break;
            }
        }
#if (PRODUCER == 1 && YCT == 1)
        if(YCTcardflag == 0)//刷了一张没在管理卡中的卡例如羊城通
        {
        }
#endif

#if (PRODUCER == 6)

        {
            // not manage cards, uoload to server for checking.
            emit _25Page_Swip(CardNum, focus_gun_);
        }
#endif
    }
}

void CarAndQRCodeVerify::AppChargingSignalOrSwopCard_Received()
{
    for(int i=0; i<5; i++)
        qDebug() << Management_CardNum[i];//看一下获取到管理卡的卡号了没有
    if(!mt.isRunning() && card->errcode == 0 && chargingData->gunmsg[focus_gun_-1].charge_state == 0)
    {
        mt.start();
    }
    AppChargingSignal();
}
void CarAndQRCodeVerify::AppChargingSignal()
{
    if(chargingData->cmd3[focus_gun_ - 1].state == 1)
    {
        Refreash_Thread[focus_gun_ - 1].appcharge = 1;
        if((chargingData->gunmsg[focus_gun_ - 1].charge_state == 1) &&
            (chargingData->gunmsg[focus_gun_ - 1].gun_conn_state == 0) &&
            (chargingData->gunmsg[focus_gun_ - 1].errcode[0] == 0))//接收到请求充电的信号
        {
            GUN_STATE[focus_gun_ - 1].Page = chargeWatchingDCPage;
            chargingData->cmd2[focus_gun_ - 1].mod = 0;
            emit appChargingDataUpdate(focus_gun_);
        }
    }
}
void CarAndQRCodeVerify::drawQR_emit()
{
    qDebug()<<QString("更新二维码")<<GUN_STATE[focus_gun_-1].m_QrCodeNum;
    ui->labelQRcode->setText(GUN_STATE[focus_gun_-1].m_QrCodeNum);
    ui->_focus_gun_->setText(QString::number(focus_gun_));
    emit onsendQRCodeValue(GUN_STATE[focus_gun_-1].m_QrCodeNum);
}

void CarAndQRCodeVerify::on_pushButton_clicked()
{
    Init_Focus();
}
void CarAndQRCodeVerify::UnlockEnableSlot()
{
    if(chargingData->cmd5[focus_gun_-1].req_state != 0){
        ui->buttonUnlock->setEnabled(false);
        ui->buttonUnlock->setStyleSheet("QPushButton{background-color: darkGray;color: rgb(25, 25, 112); font: 18px; font-weight: bold;  border-radius: 10px;}");
    }else{
        ui->buttonUnlock->setEnabled(true);
        ui->buttonUnlock->setStyleSheet("QPushButton{background-color: rgb(126, 205, 244);color: rgb(25, 25, 112); font: 18px; font-weight: bold;  border-radius: 10px;} QPushButton:pressed{background-color: qlineargradient(x1:0,x2:0,y1:0,y2:1,stop:0 rgb(126, 205, 244), stop:0.8 rgb(126, 205, 244), stop: 1 rgb(153, 153, 153)); color: rgb(25, 25, 112); font: 20px; border-radius: 10px;}");
    }
}

void CarAndQRCodeVerify::on_buttonUnlock_clicked()
{
    chargingData->cmd5[focus_gun_-1].req_state = 1;
    chargingData->cmd5[focus_gun_-1].guntab = focus_gun_;
    chargingData->cmd5[focus_gun_-1].ctrltype = 100;
}

void CarAndQRCodeVerify::on_VINcharge_clicked()
{
    Refreash_Thread[focus_gun_ - 1].VINcharge = 1;
    GUN_STATE[focus_gun_ - 1].VINflag = 1;
    GUN_STATE[focus_gun_ - 1].Page = chargeTypePage;
}
